const mixin = require('../../mixins');
// const { mfutil } = getApp()
Component({
  behaviors:[mixin],
    data: {
        currentIndex: 0,
        cornerMarkerObj: {
            left: [],
            right: []
        },
    },
    properties: {
      mf_data: Object,
    },
    attached() {
        this.lock = true; //换一换按钮锁
        this.initCornerMarker();
    },
    detached() { },
    methods: {
        initCornerMarker() {
            const { isCornerMarker, children } = this.properties.mf_data;
            if (!isCornerMarker) return;
            const { currentIndex } = this.data;
            this.setData({
                cornerMarkerObj: {
                    left: String(currentIndex + 1).split(""),
                    right: String(children.length).split(""),
                }
            })
        },
        //换一换 按钮点击事件
        changeBtnData() {
            const { switchMode } = this.properties.mf_data;
            // mfutil.utoolsParse.processEvent(this.properties.mf_data.button.EventList, this.properties.mf_data.button);
            switch (switchMode) {
                case "order":
                    this.orderChange();
                    break;
                case "random":
                    this.randomChange();
                    break;
                default: break;
            }
            this.initCornerMarker()
        },
        // 顺序切换
        orderChange() {
            if (!this.lock) return;
            this.lock = false;
            const { mf_data } = this.properties;
            let { currentIndex } = this.data;
            currentIndex += 1;
            if (currentIndex == mf_data.children.length) {
                currentIndex = 0;
            };
            this.setData(({
                currentIndex
            }), () => {
                setTimeout(() => this.lock = true, 300);
            })
        },

        // 随机切换
        randomChange() {
            if (!this.lock) return;
            this.lock = false;
            const { mf_data } = this.properties;
            let { currentIndex } = this.data;
            // 取随机数，但是不能等于当前值  可能存在爆栈 emmm 后续处理
            function random(count) {
                let num = Math.floor(Math.random() * mf_data.children.length);
                if (num == count) { return random(count) }
                return num;
            }
            const randomNum = random(currentIndex);
            this.setData(({
                currentIndex: randomNum
            }), () => {
                setTimeout(() => this.lock = true, 300);
            })
        },

    },
});
